<!DOCTYPE html>
<html class="pixel-ratio-2 retina android android-6 android-6-0">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no, minimal-ui" />
    <meta content="yes" name="apple-mobile-web-app-capable" />
    <meta content="black" name="apple-mobile-web-app-status-bar-style" />
    <meta name="format-detection" content="telephone=no" />
    <title>电表编辑</title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/mui.min.css">
    <link rel="stylesheet" href="assets/font/iconfont.css">
    <link rel="stylesheet" href="assets/mint-ui/style.css">
    <script src="assets/js/vue.min.js"></script>
    <script src="assets/js/mui.min.js"></script>
    <script src="assets/js/common.js"></script>
    <script src="assets/js/Bluetooth.js"></script>
    <script src="assets/js/dataBase.js"></script>
    <script src="assets/mint-ui/index.js"></script>
	<script src="UpGw/UpGw.js"></script>
	<script src="UpGw/utils.js"></script>
</head>
	<body>
		<div id="H5_MUI">
			<!-- 标题栏 -->
			<div class="navigation-bar">
				<div class="navigation-bar-title" >电表编辑</div>
				<div class="navigation-left">
					<div class="navigation-left-title" v-on:click="CommonUtil.clicked('meter.html','电表管理')"><li class="iconfont fs15">&#xe644;</li></div>
				</div>
				<!-- <div class="navigation-right">
					<div class="navigation-right-title">
						<i class="iconfont fs16">&#xe784;</i>
					</div>
				</div> -->
			</div>
			<!-- 内容 -->
			<div class="content">
				<form class="mui-input-group fs14">
					<div class="mui-input-row">
						<label style="width: 30%;">表名</label>
						<input type="text" class="mui-input-clear content-input" placeholder="表名" v-model="MeterName">
					</div>
					<div class="mui-input-row">
						<label style="width: 30%;">通讯地址</label>
						<input type="text" class="mui-input-clear content-input" v-model="MeterAddr" placeholder="0000000000">
					</div>
					<div class="mui-input-row">
						<label style="width: 30%;">测量点号</label>
						<input type="text" class="mui-input-clear content-input" v-model="DA" placeholder="0">
					</div>
					<div class="mui-input-row">
						<label style="width: 30%;">变比</label>
						<input type="text"  class="mui-input-clear content-input" v-model="CT" placeholder="1">
					</div>
					<div class="mui-input-row">
						<label style="width: 30%;">电表类型</label>
						<select class="mui-btn mui-btn-block" v-model="MeterTypeId" style="font-size: 14px;width:70%;text-align: left;">
							<option  v-for="(item,key) in MeterType" :key="key" :value="item.val">{{item.name}}</option>
						</select>
					</div>
					<div class="mui-input-row">
						<label style="width: 30%;">用户类型</label>
						<select class="mui-btn mui-btn-block" v-model="UserTypeId" style="font-size: 14px;width:70%;text-align: left;">
							<option  v-for="(item,key) in UserType" :key="key" :value="item.val">{{item.name}}</option>
						</select>
					</div>
					<div class="mui-input-row">
						<label style="width: 30%;">表箱地址</label>
						<input type="text" class="mui-input-clear content-input" v-model="BoxAddr" placeholder="未知">
					</div>
					
					<div class="mui-button-row">
						<button type="button" class="mui-btn"  style="background-color: #00796B;color: #FFFFFF;"  v-on:click="insertData()" >保存</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
						<button type="button" class="mui-btn mui-btn-danger" v-on:click="deleteData()">删除</button>
					</div>
				</form>
			</div>
		</div>
	</body>
</html>
<script>
	// 初始化

	var vm = new Vue({
		el: '#H5_MUI',
		data: {
			collectAddr:'000000000000',
			MeterName:' ',
			MeterAddr:'',
			CT:'1',
			DA:'1',
			MeterTypeId:' ',
			MeterTypeIdUll:'',
			UserTypeId:' ',
			UserTypeIdUll:' ',
			BoxAddr:' ',
			UserType:[
				{
					name:'考核计量点',
					val:6
				},
				{
					name:'居民用户',
					val:5
				},
				{
					name:'无需设置',
					val:0
				}
			],
			MeterType:[
				{
					name:'无需设置',
					val:0
				}
			]
		},
		created: function () {
			this.plusReady();
		},
		methods: {
			plusReady:function(){
				var that = this;
				document.addEventListener('plusready', function(){
					that.collectAddr = CommonUtil.getStorage('selCollectAddr');
					let sqlStr = "select * from Meter";
					let meterAddr = window.location.search.substr(1);
					if(meterAddr !='') 
					{
						sqlStr =  "select * from Meter where MeterAddr = '"+meterAddr+"'";
						DB.selectSQL(sqlStr,function(data){
							console.log(JSON.stringify(data));
							that.MeterName = data[0].MeterName;
							that.MeterAddr = data[0].MeterAddr;
							that.CT = data[0].CT;
							that.MeterTypeId = data[0].MeterTypeId;
							that.MeterTypeIdUll = data[0].MeterTypeId;
							that.UserTypeId = data[0].UserTypeId;
							that.UserTypeIdUll = data[0].UserTypeId;
							that.BoxAddr = data[0].BoxAddr;
							that.DA = data[0].DA;
						});
					}
				},false);
			},
			insertData:function(){
				//终端地址12位？
				if(this.MeterAddr == ''){
					mui.toast('通讯地址不能为空');
					return false;
				}
				let meterAddr = Util.padLeft(this.MeterAddr,12,'0');//补零
				let boxAddr = Util.padLeft(this.BoxAddr,12,'0');//补零
				//判断是否存在
				let that = this;
				let sqlStr = "select * from Meter where MeterAddr = '"+meterAddr+"'";
				DB.selectSQL(sqlStr,function(data){
					if(data.length >= 1){
						let UserTypeId = 0,MeterTypeId=0;
						UserTypeId = CommonUtil.getEmStr(that.UserTypeId,that.UserTypeIdUll);
						MeterTypeId = CommonUtil.getEmStr(that.MeterTypeId,that.MeterTypeIdUll);
						
						let updateStr = "update Meter set MeterName = '"+that.MeterName;
						updateStr = updateStr +"' ,MeterAddr = '"+meterAddr;
						updateStr = updateStr +"',MeterTypeId = "+MeterTypeId;
						updateStr = updateStr +" ,CT = "+that.CT;
						updateStr = updateStr +" ,UserTypeId = "+UserTypeId;
						updateStr = updateStr +" ,BoxAddr = '"+boxAddr;
						updateStr = updateStr +"' ,DA = "+that.DA;
						updateStr = updateStr +" where MeterAddr = '"+meterAddr+"'";
						
						DB.updateSQL(updateStr,function(data){
							if(data == 'success'){
								mui.toast('数据成功编辑');
								window.location.href = 'meter.html';
							}else if(data == 'fail'){
								mui.toast('数据编辑失败');
							}
						});
					}
					if(data.length <= 0){
						
						let insStr = 'INSERT INTO Meter VALUES ("';
						insStr = insStr + that.collectAddr+'", "';
						insStr = insStr + meterAddr+'", "';
						insStr = insStr + that.MeterName+'",';
						insStr = insStr + that.MeterTypeId+', ';
						insStr = insStr + that.CT+', ';
						insStr = insStr + that.UserTypeId+', "';
						insStr = insStr + boxAddr+'",';
						insStr = insStr + that.DA+')';
						
						DB.insertToSQL(insStr,function(data){
							if(data == 'success'){
								mui.toast('数据成功保存');
								window.location.href = 'meter.html';
							}else if(data == 'fail'){
								mui.toast('数据保存失败');
							}
						});
					}
				});
			},
			deleteData:function(){
				let that = this;
				let sqlStr = "select * from Meter where MeterAddr = '"+this.MeterAddr+"'";
				DB.selectSQL(sqlStr,function(data){
					if(data.length <= 0){
						mui.toast('数据不存在，请核实通讯地址');
						return false;
					}
					if(data.length >= 1){
						let btnArray = ['否', '是'];
						mui.confirm('是否删除终端'+that.MeterAddr, '删除提示', btnArray, function(e) {
							if (e.index == 1) {
								let delStr = 'delete from Meter where MeterAddr = "'+that.MeterAddr+'"';
								DB.deleteSQL(delStr,function(data){
									if(data == 'success'){
										mui.toast('数据成功删除');
										window.location.href = 'meter.html';
									}else if(data == 'fail'){
										mui.toast('数据删除失败');
									}
								});
							} else {
								mui.toast('成功取消');
							}
						})
					}
				});
			}
		}
	})
	 
</script>
